<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="名称/助记码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <!--<el-table-column prop="description" label="描述" align="center"></el-table-column>-->
                <el-table-column prop="parentMenuId" label="父菜单" align="center"></el-table-column>
                <el-table-column prop="level" label="菜单级别" align="center"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" >
                                <el-form-item label="菜单级别" prop="level" >
                                    <el-select  v-model="formData.level"  @change="parentMenuChange()" >
                                        <el-option label="一级菜单" value="1"></el-option>
                                        <el-option label="二级菜单" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="路由路径" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="图标" prop="icon" >
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单" >
                                    <el-select v-model="formData.parentMenuId"  :disabled = "parentFlag" >
                                        <el-option label="会员管理" value="1"></el-option>
                                        <el-option label="预约管理" value="5"></el-option>
                                        <el-option label="健康评估" value="11"></el-option>
                                        <el-option label="统计分析" value="13"></el-option>
                                        <el-option label="系统设置" value="15"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="描述" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level" >
                                    <el-select v-model="formData.level" @change="parentMenuChange()">
                                        <el-option label="一级菜单" value="1"></el-option>
                                        <el-option label="二级菜单" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="路由路径" prop="path" >
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="图标" prop="icon" >
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.parentMenuId" :disabled="parentFlag">
                                        <el-option label="会员管理" value="1"></el-option>
                                        <el-option label="预约管理" value="5"></el-option>
                                        <el-option label="健康评估" value="11"></el-option>
                                        <el-option label="统计分析" value="13"></el-option>
                                        <el-option label="系统设置" value="15"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="描述" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/health.js"></script>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            authos:[],
            parentFlag:false,
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}],
                level: [{required: true, message: '菜单级别为必选项', trigger: 'blur'}],
                path: [{required: true, pattern: /(^\/[1-9]-[1-9]$)|(^[1-9]\d{0,5}$)/, message: '路径格式不正确', trigger: 'blur'}],
            },
            checkBoxData:[],
            checkBoxIds:[]
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
            // 在钩子方法中获取到本地存储对象中的数据
            this.authos = localStorage.getItem("authos");
        },
        methods: {

            //菜单级别关联父菜单下拉框
            parentMenuChange(){
                var value =  this.formData.level;
                if(value !=null && value == 1) {
                   this.parentFlag = true;
                }else {
                    this.parentFlag = false;
                }
            },
            //路由路径规则校验

            //分页查询
            findPage() {
                let param = {
                    currentPage:this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/menu/findPage.do",param).then(resp => {
                    this.dataList = resp.data.rows;
                    this.pagination.total = resp.data.total;

                });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 弹出添加窗口
            handleCreate() {
                // 先要重置表单
                this.resetForm();
                this.dialogFormVisible = true;
            },

            //添加
            handleAdd() {
                // 校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid){
                        // 校验通过
                        axios.post("/menu/add.do",this.formData).then((resp) => {
                            if (resp.data.flag){
                                this.dialogFormVisible = false;
                                this.$message.success(resp.data.message);
                                this.pagination.currentPage = this.pagination.total/this.pagination.pageSize+1;
                                this.findPage();
                            } else {
                                this.$message.error(resp.data.message);
                            }
                        });
                    }else {
                        this.$message.error("表单校验失败,请检查你输入的内容是否正确!");
                        return false;
                    }
                });
            },

            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                // 向后台提交axios请求回显数据
                axios.get("/menu/findById.do?id="+row.id).then((resp) => {
                    if (resp.data.flag){
                        this.formData = resp.data.data;
                    }else {
                        this.$message.error("获取页面数据失败,请刷新页面");
                    }
                });
            },

            //编辑提交
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) =>{
                    if (valid){
                        // 发送请求保存数据
                        axios.post("/menu/update.do", this.formData).then(resp => {
                            if (resp.data.flag) {
                                this.$message.success(resp.data.message);
                                this.dialogFormVisible4Edit = false;
                            } else {
                                this.$message.error(resp.data.message);
                            }
                        }).finally(() => {
                            // 不管成功还是失败,都再查询一次
                            this.findPage();
                        });
                    }else {
                        // 校验不通过的话,弹出提示信息
                        this.$message.error("校验失败,请检查你输入的信息!");
                        return false;
                    }
                });
            },

            // 重置表单
            resetForm() {
                this.formData = {};
            },


            // 删除
            handleDelete(row) {
                this.$confirm("你确定删除这项数据吗?","提示",{type:'warning'}).then(() => {


                    // 确定删除,请求后台进行删除
                    axios.get("/menu/delete.do?id="+row.id).then((resp) => {
                        if(row.id==1||row.id==5||row.id==11||row.id==13||row.id==16){
                            this.$message.warning("您正在删除1级菜单");
                        }
                        if (resp.data.flag){
                            this.$message.success(resp.data.message);
                            this.findPage();
                        }else
                        {
                            this.$message.error("已关联角色,不能删除!");
                        }
                    });
                });
            },

            // 展示提示信息的方法
            showMessage(r){
                if (r == 'Error: Request failed with status code 403') {
                    this.$message.error('无访问权限');
                    return;
                }else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }
    })
</script>
</html>
